<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户标识用户表中的流水号" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户标识用户表中的流水号"></el-input>
      </el-form-item>
      <el-form-item label="地址名称" prop="addressName">
        <el-input v-model="dataForm.addressName" placeholder="地址名称"></el-input>
      </el-form-item>
      <el-form-item label="收货人名字" prop="consignee">
        <el-input v-model="dataForm.consignee" placeholder="收货人名字"></el-input>
      </el-form-item>
      <el-form-item label="收货人的电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="收货人的电话"></el-input>
      </el-form-item>
      <el-form-item label="收货人的手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="收货人的手机号"></el-input>
      </el-form-item>
      <el-form-item label="收货地址的标志性建筑名" prop="signBuilding">
        <el-input v-model="dataForm.signBuilding" placeholder="收货地址的标志性建筑名"></el-input>
      </el-form-item>
      <el-form-item label="收货人的最佳收货时间" prop="bestTime">
        <el-input v-model="dataForm.bestTime" placeholder="收货人的最佳收货时间"></el-input>
      </el-form-item>
      <el-form-item label="收货人的详细地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="收货人的详细地址"></el-input>
      </el-form-item>
      <!-- <el-form-item label="收货人的国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="收货人的国家"></el-input>
      </el-form-item>
      <el-form-item label="收货人的省份" prop="province">
        <el-input v-model="dataForm.province" placeholder="收货人的省份"></el-input>
      </el-form-item>
      <el-form-item label="收货人城市" prop="city">
        <el-input v-model="dataForm.city" placeholder="收货人城市"></el-input>
      </el-form-item>
      <el-form-item label="收货人的地区" prop="district"> -->
        <!-- <el-input v-model="dataForm.district" placeholder="收货人的地区"></el-input>
      </el-form-item> -->

      <el-form-item label="收货人的邮编" prop="zipcode">
        <el-input v-model="dataForm.zipcode" placeholder="收货人的邮编"></el-input>
      </el-form-item>
      <el-form-item label="收货人邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="收货人邮箱"></el-input>
      </el-form-item>
      <el-form-item label="是否启用即是否默认地址" prop="enabled">
        <el-input v-model="dataForm.enabled" placeholder="是否启用即是否默认地址"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <!-- <el-form-item label="创建即上传时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建即上传时间"></el-input>
      </el-form-item>
      <el-form-item label="修改即更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间"></el-input>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item> -->
      <el-form-item label="收货人的地区">
        <el-cascader
          size="large"
          :options="options"
          v-model="selectedOptions"
          @change="handleChange">
        </el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import isInteger from 'lodash/isInteger'
import AreaSelect from 'vue-area-linkage'
import { pca, pcaa } from 'area-data'
import { regionData } from 'element-china-area-data'
export default {
  component: {
    AreaSelect: AreaSelect
  },
  data () {
    return {
      options: regionData,
      selectedOptions: [],
      pca: pca,
      pcaa: pcaa,
      placeholders: [' ', ' ', ' '],
      visible: false,
      send_search_form: {
        orderCode: '',
        itemName: '',
        orderTime: [],
        consigneeName: '',
        state: '',
        selected: []
      },
      dataForm: {
        id: 0,
        userId: '',
        addressName: '',
        consignee: '',
        tel: '',
        mobile: '',
        signBuilding: '',
        bestTime: '',
        address: '',
        country: '',
        province: '',
        city: '',
        district: '',
        zipcode: '',
        email: '',
        enabled: '',
        operator: ''
        // createUtc: '',
        // modifyUtc: '',
        // isDeleted: '',
        // version: '',
        // createTime: '',
        // updateTime: ''
      },
      dataRule: {
        userId: [
          {
            required: true,
            message: '用户标识用户表中的流水号不能为空',
            trigger: 'blur'
          }
        ],
        addressName: [
          { required: true, message: '地址名称不能为空', trigger: 'blur' }
        ],
        consignee: [
          { required: true, message: '收货人名字不能为空', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '收货人的电话不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '收货人的手机号不能为空', trigger: 'blur' }
        ],
        signBuilding: [
          {
            required: true,
            message: '收货地址的标志性建筑名不能为空',
            trigger: 'blur'
          }
        ],
        bestTime: [
          {
            required: true,
            message: '收货人的最佳收货时间不能为空',
            trigger: 'blur'
          }
        ],
        address: [
          {
            required: true,
            message: '收货人的详细地址不能为空',
            trigger: 'blur'
          }
        ],
        country: [
          { required: true, message: '收货人的国家不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '收货人的省份不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '收货人城市不能为空', trigger: 'blur' }
        ],
        district: [
          { required: true, message: '收货人的地区不能为空', trigger: 'blur' }
        ],
        zipcode: [
          { required: true, message: '收货人的邮编不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '收货人邮箱不能为空', trigger: 'blur' }
        ],
        enabled: [
          {
            required: true,
            message: '是否启用即是否默认地址不能为空',
            trigger: 'blur'
          }
        ],
        operator: [
          { required: true, message: '操作员不能为空', trigger: 'blur' }
        ]
        // createUtc: [
        //   { required: true, message: '创建即上传时间不能为空', trigger: 'blur' }
        // ],
//     modifyUtc: [
//          { required: true, message: '修改即更新时间不能为空', trigger: 'blur' }
//        ],
//        isDeleted: [
//          { required: true, message: '是否删除不能为空', trigger: 'blur' }
//        ],
//        version: [
//          { required: true, message: '版本号不能为空', trigger: 'blur' }
//        ],
//        createTime: [
//          { required: true, message: '创建时间不能为空', trigger: 'blur' }
//        ],
//        updateTime: [
//          { required: true, message: '更新时间不能为空', trigger: 'blur' }
//        ]
      }
    }
  },
  methods: {
    // 地址三级联动
    handleChange (value) {
      console.log(value)
      this.dataForm.province = value[0]
      this.dataForm.city = value[1]
      this.dataForm.district = value[2]
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              '/generator/upkuseraddress/info' +
                (isInteger(this.dataForm.id) ? `/${this.dataForm.id}` : '')
            ),
            method: 'get'
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.selectedOptions = [ data.upkUserAddress.province, data.upkUserAddress.city, data.upkUserAddress.district ]
              this.dataForm.userId = data.upkUserAddress.userId
              this.dataForm.addressName = data.upkUserAddress.addressName
              this.dataForm.consignee = data.upkUserAddress.consignee
              this.dataForm.tel = data.upkUserAddress.tel
              this.dataForm.mobile = data.upkUserAddress.mobile
              this.dataForm.signBuilding = data.upkUserAddress.signBuilding
              this.dataForm.bestTime = data.upkUserAddress.bestTime
              this.dataForm.address = data.upkUserAddress.address
              this.dataForm.country = data.upkUserAddress.country
              this.dataForm.province = data.upkUserAddress.province
              this.dataForm.city = data.upkUserAddress.city
              this.dataForm.district = data.upkUserAddress.district
              this.dataForm.zipcode = data.upkUserAddress.zipcode
              this.dataForm.email = data.upkUserAddress.email
              this.dataForm.enabled = data.upkUserAddress.enabled
              this.dataForm.operator = data.upkUserAddress.operator
              this.dataForm.createUtc = data.upkUserAddress.createUtc
              this.dataForm.modifyUtc = data.upkUserAddress.modifyUtc
              // this.dataForm.isDeleted = data.upkUserAddress.isDeleted
              // this.dataForm.version = data.upkUserAddress.version
              // this.dataForm.createTime = data.upkUserAddress.createTime
              // this.dataForm.updateTime = data.upkUserAddress.updateTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
//          console.log(JSON.stringify(this.dataForm.selectedOptions))
//          const obj2 = Object.assign({}, this.dataForm.selectedOptions)
//          var obj = {}
//          obj = JSON.parse(JSON.stringify(this.dataForm.selectedOptions))
//          console.log(obj2)
          var params = {
            id: this.dataForm.id || undefined,
            userId: this.dataForm.userId,
            addressName: this.dataForm.addressName,
            consignee: this.dataForm.consignee,
            tel: this.dataForm.tel,
            mobile: this.dataForm.mobile,
            signBuilding: this.dataForm.signBuilding,
            bestTime: this.dataForm.bestTime,
            address: this.dataForm.address,
            country: this.dataForm.country,
            province: this.dataForm.province,
            city: this.dataForm.city,
            district: this.dataForm.district,
            zipcode: this.dataForm.zipcode,
            email: this.dataForm.email,
            enabled: this.dataForm.enabled,
            operator: this.dataForm.operator,
            createUtc: this.dataForm.createUtc,
            modifyUtc: this.dataForm.modifyUtc
            // isDeleted: this.dataForm.isDeleted,
            // version: this.dataForm.version,
            // createTime: this.dataForm.createTime,
            // updateTime: this.dataForm.updateTime
          }
          var tick = !this.dataForm.id ? this.$http({
            url: this.$http.adornUrl('/generator/upkuseraddress/save'),
            method: 'post',
            data: this.$http.adornData(params)
          }) : this.$http({
            url: this.$http.adornUrl('/generator/upkuseraddress/update'),
            method: 'post',
            data: this.$http.adornData(params)
          })
          tick.then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
